package com.qkjia.channel;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * 通道之间数据传输
 */
public class FileChannelDemo3 {

    // transferFrom()
    public static void main(String[] args) throws Exception {
        // 创建两个fileChannel 为“rw”且安全管理器的checkWrite方法拒绝对文件的写访问
        RandomAccessFile aFile = new RandomAccessFile("e:\\qkjia\\001.txt","rw");
        FileChannel fromChannel = aFile.getChannel();

        RandomAccessFile bFile = new RandomAccessFile("e:\\qkjia\\02.txt","rw");
        FileChannel toChannel = bFile.getChannel();

        // fromChannel 传输到 toChannel
        long position = 0;
        // 此频道文件的当前大小。
        long size = fromChannel.size();
        // 将字节从给定的可读字节通道传输到此通道的文件中
        toChannel.transferFrom(fromChannel,position,size);

        aFile.close();
        bFile.close();
        System.out.println("over!");
    }
}
